import 'package:flutter/material.dart';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';

import 'package:flutter_study/widgets/public/themes/index.dart';

class AppSliverListView extends StatefulWidget {
  const AppSliverListView(
      {super.key,
       this.isLast =false,
       this.slivers = const [],
      this.onLoad,
      this.onRefresh});
  final bool isLast;
  final List<Widget> slivers;
  final Future<void> Function()? onLoad;
  final Future<void> Function()? onRefresh;

   @override
  AppSliverListViewState createState() => AppSliverListViewState();
}

class AppSliverListViewState extends State<AppSliverListView> {
  late final IndicatorController controller;
  IndicatorTrigger get trigger{
    if(widget.onLoad == null || widget.isLast){
      return IndicatorTrigger.leadingEdge;
    }
    return IndicatorTrigger.bothEdges;
  }

  @override
  void initState(){
    super.initState();

    controller = IndicatorController();
  }

  @override
  void dispose(){
    controller.dispose();

    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return CustomMaterialIndicator(
      child: CustomScrollView(
        slivers: widget.slivers,
        ),
         onRefresh:() async {
          if(controller.edge == IndicatorEdge.leading){
           await widget.onRefresh?.call();
          }
          if(controller.edge == IndicatorEdge.trailing){
            await widget.onLoad?.call();
          }
         },
         trigger: trigger,
         controller: controller,
          indicatorBuilder: (context,controller){
            return Icon(
              Icons.refresh,size: 30,
              color: AppThemes.of().colors.spinkit,
            );
          });
  }
}
